<?php

require_once '../randomchar.php';
class AuthApp extends BackendApp
{
	var $_auth_mod;

	function __construct()
	{
		$this->AuthApp();
	}

	function AuthApp()
	{
		parent::BackendApp();
		$this->_auth_mod =& m('auth');
	}

	function index()
	{
		$db=&db();
		$sql="SELECT d_key,d_value FROM zys_dictionary WHERE d_code='authstatus'";
		$types=array();
		$temp=$db->getAll($sql);
		foreach ($temp as $v){
			$types[$v['d_key']]=$v['d_value'];
		}
		$types[''] = '全部';
		$this->assign('types', $types);

		$conditions = "1=1 ";
		if($_GET['type_id']!=''){
			$conditions.=" and a.a_authed=".intval($_GET['type_id']);
		}
		if($_GET['a_username']!=''){
			$conditions.=" and a.a_username like '%".$_GET['a_username']."%'";
		}
		if($_GET['zm_user_name']!=''){
			$conditions.=" and zm.user_name like '%".$_GET['zm_user_name']."%'";
		}
		if($_GET['a_vhc_code']!=''){
			$conditions.=" and a.a_vhc_code like '%".$_GET['a_vhc_code']."%'";
		}
		if($_GET['a_driver_licence']!=''){
			$conditions.=" and a.a_driver_licence like '%".$_GET['a_driver_licence']."%'";
		}
		$page   =   $this->_get_page(10);   //获取分页信息
		$sql="SELECT a.*,zm.user_name as zm_user_name,m.user_name from zys_auth a LEFT JOIN zys_member m ON a.a_auth_opid=m.user_id left join zys_member zm on a.a_userid=zm.user_id
		where ".$conditions." and a.a_type=1 ORDER BY a.a_apply_date DESC limit $page[limit]";
		$auth=$this->_auth_mod->getAll($sql);
		foreach ($auth as $k=>$v){
			$auth[$k]['a_apply_date']=local_date('Y-m-d H:i:s',$v['a_apply_date']) ;
		}
		$sql="select count(1) from zys_auth a LEFT JOIN zys_member m ON a.a_auth_opid=m.user_id left join zys_member zm on a.a_userid=zm.user_id where $conditions and a.a_type=1";
		$page['item_count']=$this->_auth_mod->getOne($sql);   //获取统计数据


		$this->_format_page($page);
		$this->assign('page_info', $page);   //将分页信息传递给视图，用于形成分页条
		$this->assign('auth', $auth);
		$this->import_resource(array('script' => 'inline_edit.js,jquery.ui/jquery.ui.js,jquery.ui/i18n/' . i18n_code() . '.js',
                                      'style'=> 'jquery.ui/themes/ui-lightness/jquery.ui.css'));

		$this->display('auth.index.html');
	}

	function pics(){
		$id=empty($_GET['id'])?0:intval($_GET['id']);
		if($id==0){
			$this->show_warning('用户不存在','','index.php?app=auth');
			return ;
		}
		$db=&db();
		$sql="SELECT * FROM zys_auth_pic WHERE ap_a_tid=".$id;
		$pics=$db->getAll($sql);
		$this->assign('pics', $pics);
		$this->display('auth.pics.html');
	}


	//删除
	function drop()
	{

		$arr = isset($_GET['id']) ? trim($_GET['id']) : '';

		$arrs=explode(',', $arr);


		if($arrs&&count($arrs)>0){
			foreach ($arrs as $v){
				$temp=$this->_auth_mod->get($v);
				if(empty($temp['a_authed'])){
					$this->_auth_mod->drop($v);
				}
			}
		}

		$this->show_message('删除成功' ,'',   'index.php?app=auth');

	}
	//认证
	function authentication()
	{
		$db=&db();
		$arr = isset($_GET['id']) ? trim($_GET['id']) : '';

		$arrs=explode(',', $arr);
		if($arrs&&count($arrs)>0){
			foreach ($arrs as $v){
				$this->_auth_mod->edit(" a_tid=$v and a_authed=0 ",array(
				'a_authed'=>1,
				'a_auth_opid'=>$this->visitor->get('user_id'),
				'a_auth_date'=>gmtime()
				));

				$sql="select * from zys_auth where a_tid=".$v;
				$res=$db->getRow($sql);
				if($res&&$res['a_vhc_code']&&$res['a_type']==1){
					$sql="update zys_member set vhcCode='".$res['a_vhc_code']."',type=1 where user_id=".$res['a_userid'];
					$db->query($sql);

					//记录车辆信息
					$randCharObj =new RandChar();
					$uuid=$randCharObj->getRandChar(20);
					$user=null;
					while(true){
						$sql="select * from zys_vehicle where uuid='".$uuid."'";
						$user=$db->getRow($sql);
						if(empty($user)){
							break;
						}
						else{
							$user=null;
							$uuid=$randCharObj->getRandChar(20);
						}
					}
					$sql="insert zys_vehicle set vhc_userid=".$res['a_userid'].",vhc_atid=".$res['a_tid'].",uuid='".$uuid."',vhc_code='".$res['a_vhc_code'].
						"',vhc_model='".$res['a_vhc_model']."',vhc_size='".$res['a_vhc_size']."',vhc_weight='".$res['a_vhc_size']."'";
					$db->query($sql);

				}elseif($res&&$res['a_type']==0){
					$sql="update zys_member set real_name='".$res['a_username']."' where user_id=".$res['a_userid'];
					$db->query($sql);
				}

                if($this->checkUrl('http://192.168.0.180:8074/PlantFormService.asmx?WSDL')) {
                    $client = new soapclient('http://192.168.0.180:8074/PlantFormService.asmx?WSDL');
                    $client->soap_defencoding = 'utf-8';
                    $client->decode_utf8 = false;
                    $client->xml_encoding = 'utf-8';
                    $param = array('PJson'=>'{"CarNo": "'.$res['a_vhc_code'].'","CarType": "'.$res['a_vhc_model'].'","CarDriver": "",
                        "DriverTel": "","CarLenth": "'.$res['a_vhc_size'].'","CarLoad": "'.$res['a_vhc_weight'].'"}');

                    $result = $client->__Call("SetCarNo", array($param));
                    if (is_soap_fault($result)) {
                        trigger_error("SOAP Fault: (faultcode: {$result->faultcode}, faultstring: {$result->faultstring})", E_USER_ERROR);
                    } else {
                        $result = json_decode(json_encode($result->SetCarNoResult), TRUE);
                        $result = json_decode($result, true);
                        if($result['status']!=1){
                            $this->show_warning('同步数据失败','','index.php?app=auth');
                            return;
                        }
                    }
                }else{
                    $this->show_warning('业务系统无法链接','','index.php?app=auth');
                    return;
                }
			}
		}else{
			if (!$this->_auth_mod->edit(" a_tid=$arr ",array('a_authed'=>1))){
				$this->show_warning($this->_auth_mod->get_error());
				return;
			}
		}
		$this->show_message('认证成功' ,'',   'index.php?app=auth');

	}
	//退回
	function refuse()
	{
		$db=&db();
		$arr = isset($_POST['id']) ? trim($_POST['id']) : '';
		$content = isset($_POST['content']) ? trim($_POST['content']) : '';
		$arrs=explode(',', $arr);
		try {
			if($arrs&&count($arrs)>0){
				foreach ($arrs as $v){
					$this->_auth_mod->edit(" a_tid=$v and a_authed=0 ",array(
						'a_auth_content'=>$content,
						'a_authed'=>2,
						'a_auth_opid'=>$this->visitor->get('user_id'),
						'a_auth_date'=>gmtime()
					));
				}
			}else{
				if (!$this->_auth_mod->edit(" a_tid=$arr ",array('a_authed'=>2,'a_auth_content'=>$content,'a_auth_opid'=>$this->visitor->get('user_id'),'a_auth_date'=>gmtime()))){
					throw new Exception($this->_auth_mod->get_error());
					return;
				}
			}
			$this->json_result('', '退回成功' );
		} catch ( Exception $e ) {
			$this->json_error ($e->getMessage());
			exit ();
		}
	}

}
